/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IExtendRetrieveDefaultAssembler;
import com.inspur.edp.bff.api.manager.context.RetrieveDefaultContext;
import com.inspur.edp.bff.spi.action.retrievedefault.AbstractRetrieveDefaultAction;
import com.inspur.edp.bff.spi.action.retrievedefault.AfterRetrieveDefaultAction;
import com.inspur.edp.bff.spi.action.retrievedefault.BeforeRetrieveDefaultAction;
import com.inspur.edp.bff.spi.action.retrievedefault.DefaultRetrieveDefaultAction;
import com.inspur.edp.bff.spi.event.retrievedefault.BffRetrieveDefaultEventBroker;

import java.util.ArrayList;

public abstract class AbstractRetrieveDefaultAssembler implements IExtendRetrieveDefaultAssembler {

    protected abstract ArrayList<BeforeRetrieveDefaultAction> getBeforeRetrieveDefaultActions(RetrieveDefaultContext createContext);

    protected abstract ArrayList<AbstractRetrieveDefaultAction> getRetrieveDefaultActions(RetrieveDefaultContext createContext);

    protected abstract ArrayList<AfterRetrieveDefaultAction> getAfterRetrieveDefaultActions(RetrieveDefaultContext createContext);

    public final void doBeforeRetrieveDefault(IFSManagerContext context, RetrieveDefaultContext createContext) {
        ArrayList<BeforeRetrieveDefaultAction> actions = getBeforeRetrieveDefaultActions(createContext);
        if (actions == null || actions.size() < 1) {
            dealNoneBeforeAction(actions, context);
            return;
        }
        try {
            BffRetrieveDefaultEventBroker.fireB4ExecuteRetrieveDefaultActions(context.getBffManager(), actions);
            dealBeforeRetrieveDefault(actions, context, createContext);
            BffRetrieveDefaultEventBroker.fireAfterExecuteRetrieveDefaultActions(context.getBffManager(), actions);
        } catch (Exception e) {
            BffRetrieveDefaultEventBroker.fireExceptionStop(e);
            throw e;
        }

    }

    private final void dealNoneBeforeAction(ArrayList<BeforeRetrieveDefaultAction> actions, IFSManagerContext context) {
        try {
            BffRetrieveDefaultEventBroker.fireB4ExecuteRetrieveDefaultActions(context.getBffManager(), actions);
            BffRetrieveDefaultEventBroker.fireAfterExecuteRetrieveDefaultActions(context.getBffManager(), actions);
        } catch (Exception e) {
            BffRetrieveDefaultEventBroker.fireExceptionStop(e);
            throw e;
        }
    }

    private final void dealBeforeRetrieveDefault(ArrayList<BeforeRetrieveDefaultAction> actions, IFSManagerContext context, RetrieveDefaultContext createContext) {
        for (BeforeRetrieveDefaultAction action : actions) {
            try {
                BffRetrieveDefaultEventBroker.fireB4BeforeRetrieveDefaultAction(context.getBffManager(), action, createContext);
                action.deal(context);
                BffRetrieveDefaultEventBroker.fireAfterBeforeRetrieveDefaultAction(context.getBffManager(), createContext);
            } catch (Exception e) {
                BffRetrieveDefaultEventBroker.fireExceptionStop(e);
                throw e;
            }

        }
    }

    public final void doRetrieveDefault(IFSManagerContext context, RetrieveDefaultContext createContext) {
        ArrayList<AbstractRetrieveDefaultAction> actions = getRetrieveDefaultActions(createContext);
        if (actions == null || actions.size() < 1) {
            //兼容原有VO没有设置内置Retrieve，但设置了BeforeRetrieve或AfterRetrieve
            actions = new ArrayList<AbstractRetrieveDefaultAction>();
            DefaultRetrieveDefaultAction detaultAction = new DefaultRetrieveDefaultAction(createContext);
            actions.add(detaultAction);
        }

        try {
            BffRetrieveDefaultEventBroker.fireB4ExecuteAbstractRetrieveDefaultActions(context.getBffManager(), actions);
            dealRetrieveDefault(actions, context, createContext);
            BffRetrieveDefaultEventBroker.fireAfterExecuteAbstractRetrieveDefaultActions(context.getBffManager(), actions);
        } catch (Exception e) {
            BffRetrieveDefaultEventBroker.fireExceptionStop(e);
            throw e;
        }

    }

    private final void dealRetrieveDefault(ArrayList<AbstractRetrieveDefaultAction> actions, IFSManagerContext context, RetrieveDefaultContext createContext) {
        for (AbstractRetrieveDefaultAction action : actions) {
            try {
                BffRetrieveDefaultEventBroker.fireB4AbstractRetrieveDefaultAction(context.getBffManager(), action, createContext);
                action.deal(context);
                BffRetrieveDefaultEventBroker.fireAfterAbstractRetrieveDefaultAction(context.getBffManager(), createContext);
            } catch (Exception e) {
                BffRetrieveDefaultEventBroker.fireExceptionStop(e);
                throw e;
            }
        }
    }

    public final void doAfterRetrieveDefault(IFSManagerContext context, RetrieveDefaultContext createContext) {
        ArrayList<AfterRetrieveDefaultAction> actions = getAfterRetrieveDefaultActions(createContext);
        if (actions == null || actions.size() < 1) {
            dealNoneAfterAction(actions, context);
            return;
        }
        try {
            BffRetrieveDefaultEventBroker.fireB4ExecuteAfterRetrieveDefaultActions(context.getBffManager(), actions);
            dealAfterRetrieveDefault(actions, context, createContext);
            BffRetrieveDefaultEventBroker.fireAfterExecuteAfterRetrieveDefaultActions(context.getBffManager(), actions);
        } catch (Exception e) {
            BffRetrieveDefaultEventBroker.fireExceptionStop(e);
            throw e;
        }

    }

    private final void dealNoneAfterAction(ArrayList<AfterRetrieveDefaultAction> actions, IFSManagerContext context) {
        try {
            BffRetrieveDefaultEventBroker.fireB4ExecuteAfterRetrieveDefaultActions(context.getBffManager(), actions);
            BffRetrieveDefaultEventBroker.fireAfterExecuteAfterRetrieveDefaultActions(context.getBffManager(), actions);
        } catch (Exception e) {
            BffRetrieveDefaultEventBroker.fireExceptionStop(e);
            throw e;
        }
    }

    private final void dealAfterRetrieveDefault(ArrayList<AfterRetrieveDefaultAction> actions, IFSManagerContext context, RetrieveDefaultContext createContext) {
        for (AfterRetrieveDefaultAction action : actions) {
            try {
                BffRetrieveDefaultEventBroker.fireB4AfterRetrieveDefaultAction(context.getBffManager(), action, createContext);
                action.deal(context);
                BffRetrieveDefaultEventBroker.fireAfterAfterRetrieveDefaultAction(context.getBffManager(), createContext);
            } catch (Exception e) {
                BffRetrieveDefaultEventBroker.fireExceptionStop(e);
                throw e;
            }
        }
    }
}
